package com.ruoyi.central.controller;

import java.util.List;

import com.ruoyi.central.publisher.PublisherUtils;
import com.ruoyi.central.publisher.event.ArtProcessEvent;
import com.ruoyi.central.service.ITopCentralProductTypeService;
import com.ruoyi.common.enums.SignEnum;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.service.ServiceUtils;
import com.ruoyi.common.validation.group.InsertGroup;
import com.ruoyi.common.validation.group.UpdateGroup;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.utils.bean.SelectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.central.domain.TopCentralArtProcess;
import com.ruoyi.central.service.ITopCentralArtProcessService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.validation.group.UpdateBatchGroup;
import com.ruoyi.common.core.domain.StateVo;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 工艺Controller
 * 
 * @author ruoyi
 * @date 2022-03-02
 */
@Controller
@RequestMapping("/central/centralArtProcess")
public class TopCentralArtProcessController extends BaseController
{
    private String prefix = "central/centralArtProcess";

    @Autowired
    private ITopCentralArtProcessService topCentralArtProcessService;

    @Autowired
    private ITopCentralProductTypeService productTypeService;

    @RequiresPermissions("central:centralArtProcess:view")
    @GetMapping()
    public String centralArtProcess()
    {
        return prefix + "/centralArtProcess";
    }

    /**
     * 查询工艺列表
     */
    @RequiresPermissions("central:centralArtProcess:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(TopCentralArtProcess topCentralArtProcess)
    {
        startPage();
        List<TopCentralArtProcess> list = topCentralArtProcessService.selectList(topCentralArtProcess);
        ServiceUtils.complete(list,productTypeService,(productTypeMap,item)->{
            item.setProductType(productTypeMap.get(item.getProductTypeId()));
        });
        return getDataTable(list);
    }

    /**
     * 查询简单的工艺列表 用于select选择器
     */
    @GetMapping("/simpleList")
    @ResponseBody
    public AjaxResult simpleList(TopCentralArtProcess topCentralArtProcess){

        Collection<TopCentralArtProcess> topCentralArtProcessList = topCentralArtProcessService.selectAllMap().values();

        if(StringUtils.isNotNull(topCentralArtProcess.getProductTypeId())){
            Integer productTypeId = topCentralArtProcess.getProductTypeId();
            topCentralArtProcessList = topCentralArtProcessList.stream().filter((item)->StringUtils.equals(item.getProductTypeId(),productTypeId)).collect(Collectors.toList());
        }

        return AjaxResult.success(SelectUtils.buildSelectList(topCentralArtProcessList,TopCentralArtProcess::getId,TopCentralArtProcess::getName));
    }

    /**
     * 获取工艺详细信息
     */
    @RequiresPermissions("central:centralArtProcess:query")
    @GetMapping(value = "/{id}")
    @ResponseBody
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(topCentralArtProcessService.selectById(id));
    }

    /**
     * 获取自增排序值
     * @param topCentralArtProcess 过滤条件
     * @return
     */
    @GetMapping("/getIncrementWeight")
    @ResponseBody
    public AjaxResult getIncrementWeight(TopCentralArtProcess topCentralArtProcess){
        int weigth = topCentralArtProcessService.getMaxOfWeight(topCentralArtProcess);
        return AjaxResult.success(++weigth);
    }

    /**
     * 下载工艺导入模板
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<TopCentralArtProcess> util = new ExcelUtil<>(TopCentralArtProcess.class);
        util.importTemplateExcel(response, "工艺数据");
    }

    /**
     * 导入工艺数据
     * @param file 工艺数据文件
     * @param updateSupport 是否为更新
     */
    @Log(title = "工艺", businessType = BusinessType.IMPORT)
    @RequiresPermissions("central:centralArtProcess:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TopCentralArtProcess> util = new ExcelUtil<>(TopCentralArtProcess.class);
        List<TopCentralArtProcess> topCentralArtProcessList = util.importExcel(file.getInputStream());

        if(updateSupport){
            if(topCentralArtProcessService.updateBatchById(topCentralArtProcessList)){
                return AjaxResult.success("导入工艺数据覆盖成功！");
            }
            return AjaxResult.error("导入工艺数据覆盖失败！");
        }
        if(topCentralArtProcessService.insertBatch(topCentralArtProcessList)){
            return AjaxResult.success("导入工艺数据新增成功！");
        }
        return AjaxResult.error("导入工艺数据新增失败！");
    }

    /**
     * 导出工艺列表
     */
    @RequiresPermissions("central:centralArtProcess:export")
    @Log(title = "工艺", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TopCentralArtProcess topCentralArtProcess)
    {
        List<TopCentralArtProcess> list = topCentralArtProcessService.selectList(topCentralArtProcess);
        ExcelUtil<TopCentralArtProcess> util = new ExcelUtil<TopCentralArtProcess>(TopCentralArtProcess.class);
        return util.exportExcel(list, "工艺数据");
    }

    /**
     * 新增工艺
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存工艺
     */
    @RequiresPermissions("central:centralArtProcess:add")
    @Log(title = "工艺", businessType = BusinessType.INSERT)
    @PostMapping
    @ResponseBody
    public AjaxResult addSave(@Validated(InsertGroup.class) @RequestBody TopCentralArtProcess topCentralArtProcess)
    {
        return toAjax(topCentralArtProcessService.insert(topCentralArtProcess));
    }

    /**
     * 修改工艺
     */
    @RequiresPermissions("central:centralArtProcess:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        TopCentralArtProcess topCentralArtProcess = topCentralArtProcessService.selectById(id);
        mmap.put("topCentralArtProcess", topCentralArtProcess);
        return prefix + "/edit";
    }

    /**
     * 修改保存工艺
     */
    @RequiresPermissions("central:centralArtProcess:edit")
    @Log(title = "工艺", businessType = BusinessType.UPDATE)
    @PutMapping
    @ResponseBody
    public AjaxResult editSave(@Validated(UpdateGroup.class) @RequestBody TopCentralArtProcess topCentralArtProcess)
    {
        PublisherUtils.publishEvent(ArtProcessEvent.class,topCentralArtProcess, SignEnum.UPDATE);
        return toAjax(topCentralArtProcessService.updateById(topCentralArtProcess));
    }

    /**
     * 修改工艺 状态 根据 ids 主键
     */
    @RequiresPermissions("central:centralArtProcess:edit")
    @Log(title = "工艺", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStateByIds")
    @ResponseBody
    public AjaxResult updateStateByIds(@Validated(value= UpdateBatchGroup.class)@RequestBody StateVo<Integer> stateVo)
    {

        PublisherUtils.publishEvent(ArtProcessEvent.class,ServiceUtils.toBeanList(stateVo.getIds(),
                TopCentralArtProcess.class,(artProcess,artProcessId)->{artProcess.setId(artProcessId);artProcess.setState(stateVo.getState());}), SignEnum.UPDATE);
        return toAjax(topCentralArtProcessService.updateStateByIds(stateVo.getIds(),stateVo.getState()));
    }

    /**
     * 删除工艺
     */
    @RequiresPermissions("central:centralArtProcess:remove")
    @Log(title = "工艺", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ResponseBody
    public AjaxResult deleteByIds(@PathVariable("ids") List<Integer> ids)
    {
            return toAjax(topCentralArtProcessService.deleteByIds(ids));
    }
}
